import React, { useState } from 'react'
import NavBar from '@/components/NavBar'
import styles from './index.module.scss'
import { useFormik } from 'formik'
import * as Yup from 'yup'
import classNames from 'classnames'
import { useDispatch } from 'react-redux'
import { login, sendValidationCode } from '@/store/actions/login'
import { Toast } from 'antd-mobile'
import { useNavigate } from 'react-router-dom'

export default function Login() {
  const navigate = useNavigate()
  const [time, setTime] = useState(0)
  const formik = useFormik({
    initialValues: {
      mobile: '13900001111',
      code: '246810',
    },
    async onSubmit(value) {
      // console.log(value)
      await dispatch(login(value))
      Toast.show({
        icon: 'success',
        content: '登录成功',
      })
      navigate('/')
    },
    validationSchema: Yup.object().shape({
      mobile: Yup.string()
        .required('手机号不能为空')
        .matches(/^1[3-9]\d{9}$/, '手机号格式错误'),
      code: Yup.string()
        .required('验证码不能为空')
        .matches(/^\d{6}$/, '验证码6个数字'),
    }),
  })
  const dispatch = useDispatch()
  const sendSMSCode = async () => {
    if (time > 0) return
    if (!/^1[3-9]\d{9}$/.test(mobile)) {
      setTouched({
        mobile: true,
      })
      return
    }

    dispatch(sendValidationCode(mobile))
    Toast.show({
      icon: 'success',
      content: '验证码获取成功',
    })
    setTime(60)

    let timer = setInterval(() => {
      setTime((time) => {
        if (time === 1) {
          clearInterval(timer)
        }
        return time - 1
      })
      // console.log(time)
      // if (time === 0) {
      //   clearInterval(timer)
      // }
    }, 1000)
  }
  const {
    values: { mobile, code },
    handleChange,
    handleSubmit,
    handleBlur,
    errors,
    touched,
    isValid,
    setTouched,
  } = formik
  return (
    <div className={styles.root}>
      <NavBar>登录</NavBar>
      <div className="content">
        {/* 标题 */}
        <h3>短信登录</h3>
        <form onSubmit={handleSubmit}>
          {/* 手机号输入框 */}
          <div className="input-item">
            <div className="input-box">
              <input
                className="input"
                name="mobile"
                placeholder="请输入手机号"
                autoComplete="off"
                value={mobile}
                maxLength={11}
                onChange={handleChange}
                onBlur={handleBlur}
              />
            </div>
            {touched.mobile && errors.mobile ? (
              <div className="validate">{errors.mobile}</div>
            ) : null}
          </div>

          {/* 短信验证码输入框 */}
          <div className="input-item">
            <div className="input-box">
              <input
                className="input"
                name="code"
                placeholder="请输入验证码"
                maxLength={6}
                autoComplete="off"
                value={code}
                onChange={handleChange}
                onBlur={handleBlur}
              />
              <div className="extra" onClick={sendSMSCode}>
                {time === 0 ? '获取验证码' : time + 's重新获取'}
              </div>
            </div>
            {touched.code && errors.code ? (
              <div className="validate">{errors.code}</div>
            ) : null}
          </div>

          {/* 登录按钮 */}
          <button
            type="submit"
            className={classNames('login-btn', isValid ? '' : 'disabled')}
            disabled={!isValid}
          >
            登录
          </button>
        </form>
      </div>
    </div>
  )
}
